package service

import (
	"api-gateway/basic/config"
	"api-gateway/handler/request"
	"api-gateway/pkg"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"strconv"
	"time"
)

func Sms(c *gin.Context) {
	var form request.Sms
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	code := rand.Intn(9000000) + 1000000
	sms, err := pkg.SendSms(form.Phone, strconv.Itoa(code))
	if err != nil {
		return
	}
	if sms.Code != 2 {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "验证码发送失败"})
		return
	}
	config.Rdb.Set(config.Ctx, "smsCode_"+form.Phone, code, time.Minute*5)
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "验证码发送成功", "data": gin.H{}})
	return
}
